package com.xindaoapp.happypet.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.Comment;

public class CommentListAdapter extends MyBaseAdapter<Comment> {
	private final Context mContext;
	private final List<Comment> mList;

	public CommentListAdapter(Context context, List<Comment> mList, int resId) {
		super(context, mList, resId);
		this.mContext = context;
		this.mList = mList;
	}

	public void addData(List<Comment> mList) {
		this.mList.addAll(mList);
		notifyDataSetChanged();
	}

	public void clearData(List<Comment> mList) {
		this.mList.clear();
		notifyDataSetChanged();
	}

	public void clearAllData() {
		this.mList.clear();
		notifyDataSetChanged();
	}

	private int getWidth() {

		WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
		return wm.getDefaultDisplay().getWidth();
	}

	@Override
	public View getView(int position, View contentView, ViewGroup parent, Comment t) {
		ViewHolder holder = (ViewHolder) contentView.getTag();
		final Comment info = mList.get(position);

		if (null == holder) {
			holder = new ViewHolder();
			holder.ll_star = (LinearLayout) contentView.findViewById(R.id.comment_star);
			holder.content = (TextView) contentView.findViewById(R.id.comment_count);
			holder.name = (TextView) contentView.findViewById(R.id.comment_user_name);
			holder.time = (TextView) contentView.findViewById(R.id.comment_time);
			contentView.setTag(holder);
		}

		holder.ll_star.removeAllViews();
		for (int i = 0; i < 5; i++) {
			LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			lp.leftMargin = 5;
			ImageView iv = new ImageView(mContext);
			if (i < Integer.parseInt(info.comment_rank)) {
				iv.setBackgroundResource(R.drawable.star);
			} else {
				iv.setBackgroundResource(R.drawable.star_normal);
			}
			iv.setLayoutParams(lp);
			holder.ll_star.addView(iv);
		}

		holder.content.setText("心得:" + info.content);
		holder.name.setText(info.user_name + ":");
		holder.time.setText(info.add_time.split(" ")[0]);

		return contentView;
	}

	class ViewHolder {
		LinearLayout ll_star;
		TextView content;
		TextView name;
		TextView time;
	}
}
